package org.kim.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Utils {

    public static String getMd5(String message){
        try {
            // create instance of MessageDigest
            MessageDigest md = MessageDigest.getInstance("MD5");

            // calculate message's digest
            byte[] digest = md.digest(message.getBytes());

            // transfer digit to Hexadecimal string
            String hexString = bytesToHex(digest);
            return hexString;

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }


    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
}
